//
//  ShowImageView.m
//  XGT
//
//  Created by  程 强 on 15/6/8.
//  Copyright (c) 2015年 Demon. All rights reserved.
//

#import "ShowImageView.h"
#import "JTCSM.h"
//#import "UIImageView+WebCache.h"
@interface ShowImageView ()
{
    UILabel *titleLabel;
    UILabel *numberLabel;
    UIView *rightView;
    BOOL isshow;
}

@end

@implementation ShowImageView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
//static NSInteger page = 0;

- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KVIEWwith, frame.size.height)];
        self.scrollview.backgroundColor = [UIColor whiteColor];
        self.scrollview.userInteractionEnabled = YES;
        self.scrollview.bounces = NO;
        [self addSubview:self.scrollview];
        
        
        self.pageControl = [[UIPageControl alloc]init];
        _pageControl.currentPage = 0;
        [self addSubview:self.pageControl];
        //    图片的宽
        CGFloat imageW = self.scrollview.frame.size.width;
        //    CGFloat imageW = 300;
        //    图片高
        CGFloat imageH = self.scrollview.frame.size.height;
        
        titleLabel = [UILabel initWithFrame:CGRectMake(10, imageH-35, imageW/2, 24) text:@"" font:15 textAlignment:NSTextAlignmentLeft texcolor:[UIColor whiteColor] backgroundColor:nil];
        [self addSubview:titleLabel];
        [self bringSubviewToFront:titleLabel];
        
        rightView= [[UIView alloc]initWithFrame:CGRectMake(imageW-60, imageH-35, 84, 24)];
        rightView.backgroundColor = RGBACOLOR(0, 0, 0, 0.7);
        [df layerWithView:rightView radius:12 width:0 color:nil];
        [self addSubview:rightView];
        [self bringSubviewToFront:rightView];
        numberLabel = [UILabel initWithFrame:CGRectMake(0, 0, 60, 24) text:@"" font:15 textAlignment:NSTextAlignmentCenter texcolor:[UIColor whiteColor] backgroundColor:nil];
        [rightView addSubview:numberLabel];
        rightView.hidden = YES;
        
//        [self viewDidLoadWithImageArray:array title:text];
    }
    
    self.backgroundColor = [UIColor whiteColor];
//    UILabel *label =[UILabel initWithFrame:CGRectMake(0, self.scrollview.frame.size.height-labelHeight, self.scrollview.frame.size.width, labelHeight) text:[NSString stringWithFormat:@"%@次推荐成功",text] font:18 textAlignment:NSTextAlignmentCenter texcolor:[UIColor whiteColor] backgroundColor:RGBACOLOR(255, 68, 0, 1)];
//    [self addSubview:label];

    return self;
}

- (void)viewDidLoadWithImageArray:(NSArray *)array
 {
     CGFloat space = 0;
     //    图片的宽
     CGFloat imageW = self.scrollview.frame.size.width-2*space;
     //    CGFloat imageW = 300;
     //    图片高
     CGFloat imageH = self.scrollview.frame.size.height;
     //    图片的Y
     CGFloat imageY = 0;
     if (array.count == 0 || array ==nil) {
         rightView.hidden = YES;
         
         _imageView = [[UIImageView alloc] init];
         _imageView.userInteractionEnabled = YES;
         [df layerWithView:_imageView radius:.1 width:0 color:nil];
         [df addTapGestureRecognizerWithTarget:self action:@selector(tiaozhuanView:) andaddView:_imageView];
         _imageView.contentMode = UIViewContentModeScaleAspectFill;
         //        图片X
         CGFloat imageX =0;
         //        设置frame
         _imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
         //        设置图片
        
         [_imageView sd_setImageWithURL:KImageUrl(@"") placeholderImage:[UIImage imageNamed:@"imagenull2.jpg"]];
         //        隐藏指示条
         self.scrollview.showsHorizontalScrollIndicator = NO;
         
         [self.scrollview addSubview:_imageView];
         return;
     }
     [_imageView removeFromSuperview];
     rightView.hidden = NO;
     for (int i = 0; i<array.count; i++) {
         UIImageView *imagev = [self.scrollview viewWithTag:300+i];
         [imagev removeFromSuperview];
     }
     
     
     
      //   1.添加5张图片
     for (int i = 0; i < array.count; i++) {
         
        _imageView = [[UIImageView alloc] init];
         _imageView.userInteractionEnabled = YES;
         _imageView.tag = 300+i;
         [df layerWithView:_imageView radius:.1 width:0 color:nil];
        [df addTapGestureRecognizerWithTarget:self action:@selector(tiaozhuanView:) andaddView:_imageView];
         _imageView.contentMode = UIViewContentModeScaleAspectFill;
//        图片X
        CGFloat imageX = i * (imageW+2*space)+space;
//        设置frame
        _imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
//        设置图片
         NSString *name = [array[i] objectForKey:@"imgUrl"];
        [_imageView sd_setImageWithURL:KImageUrl(name) placeholderImage:[UIImage imageNamed:@"imagenull2.jpg"]];
//         [df layerWithView:_imageView radius:4 width:0 color:nil];
//         _imageView.image = [UIImage imageNamed:name];
//        隐藏指示条
        self.scrollview.showsHorizontalScrollIndicator = NO;

        [self.scrollview addSubview:_imageView];
        
    }
     NSDictionary *dict = array[0];
     NSString *title = [dict objectForKey:@"imgTitle "];
     titleLabel.text = title;
     numberLabel.text = [NSString stringWithFormat:@"1/%ld",(long)array.count];
     
 //    2.设置scrollview的滚动范围
     CGFloat contentW = array.count *(imageW+2*space);
     //不允许在垂直方向上进行滚动
     self.scrollview.contentSize = CGSizeMake(contentW, imageH);

 //    3.设置分页
     self.scrollview.pagingEnabled = YES;

 //    4.监听scrollview的滚动
     self.scrollview.delegate = self;

     CGFloat width = 16*array.count;
     self.pageControl.frame = CGRectMake((KVIEWwith-width)/2, self.scrollview.frame.size.height-20, width, 10);
     _pageControl.numberOfPages = [array count];
     isshow = YES;
     self.array = array;
     [self.timer invalidate];
     [self addTimer];
 }

- (void)nextImage
 {
     int page = (int)self.pageControl.currentPage;
     if (page == _array.count-1) {
         page = 0;
     }else
        {
            page++;
        }

 //  滚动scrollview
     CGFloat x = page * self.scrollview.frame.size.width;
     self.scrollview.contentOffset = CGPointMake(x, 0);
    
 }

 // scrollview滚动的时候调用
 - (void)scrollViewDidScroll:(UIScrollView *)scrollView
 {
 //    计算页码
 //    页码 = (contentoffset.x + scrollView一半宽度)/scrollView宽度
     CGFloat scrollviewW =  scrollView.frame.size.width;
     CGFloat x = scrollView.contentOffset.x;
     int page = (x + scrollviewW / 2) /  scrollviewW;
     self.pageControl.currentPage = page;
     if (isshow) {
         NSDictionary *dict = _array[page];
         NSString *title = [dict objectForKey:@"imgTitle "];
         titleLabel.text = title;
         numberLabel.text = [NSString stringWithFormat:@"%d/%ld",page+1,(long)_array.count];
     }
     
 }

 // 开始拖拽的时候调用
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
 {
 //    关闭定时器(注意点; 定时器一旦被关闭,无法再开启)
     [self.timer invalidate];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
   //    开启定时器
    [self.timer invalidate];
       [self addTimer];
}
- (void)addTimer{
  
       self.timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
       [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)removeTimer
{
        [self.timer invalidate];
}

- (void)tiaozhuanView:(UITapGestureRecognizer *)tap{
    [_delegate touchImageView:[tap view].tag-300];
}


- (void)home_viewDidLoadWithImageArray:(NSArray *)array
{
    
    rightView.hidden = YES;
    for (int i = 0; i<array.count; i++) {
        UIImageView *imagev = [self.scrollview viewWithTag:400+i];
        [imagev removeFromSuperview];
    }
    CGFloat space = 0;
    //    图片的宽
    CGFloat imageW = self.scrollview.frame.size.width-2*space;
    //    CGFloat imageW = 300;
    //    图片高
    CGFloat imageH = self.scrollview.frame.size.height;
    //    图片的Y
    CGFloat imageY = 0;
    
    
    //   1.添加5张图片
    for (int i = 0; i < array.count; i++) {
        
        _imageView = [[UIImageView alloc] init];
        _imageView.userInteractionEnabled = YES;
        _imageView.tag = 400+i;
        [df layerWithView:_imageView radius:.1 width:0 color:nil];
        [df addTapGestureRecognizerWithTarget:self action:@selector(tiaozhuanView:) andaddView:_imageView];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        //        图片X
        CGFloat imageX = i * (imageW+2*space)+space;
        //        设置frame
        _imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
        //        设置图片
        _imageView.image = [UIImage imageNamed:array[i]];
//        [df layerWithView:_imageView radius:4 width:0 color:nil];
 
        self.scrollview.showsHorizontalScrollIndicator = NO;
        
        [self.scrollview addSubview:_imageView];
        
        UILabel *label = [UILabel initWithFrame:CGRectMake(0,imageH/3,imageW,40) text:i==0?@"房子卖得快，房主多卖钱":@"一家帮你快速卖房的网站" font:28 textAlignment:NSTextAlignmentCenter texcolor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0] backgroundColor:nil];
   
        [_imageView addSubview:label];
       
        
        UILabel *label1 =  [UILabel initWithFrame:CGRectMake(0,KVIEW_Y(label)+5,imageW,20) text:i==0?@"通过\"秒卖房\"卖二手房，省去看房烦恼， ":@"房主委托，全城中介帮卖，省时省心卖的快" font:14 textAlignment:NSTextAlignmentCenter texcolor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0] backgroundColor:nil];
    
        [_imageView addSubview:label1];
        
    
        UILabel *label2 = [UILabel initWithFrame:CGRectMake(0,KVIEW_Y(label1),imageW,20) text:i==0?@"出售速度快，出售价格高。":@"" font:14 textAlignment:NSTextAlignmentCenter texcolor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0] backgroundColor:nil];
        [_imageView addSubview:label2];
        
    }
    
    //    2.设置scrollview的滚动范围
    CGFloat contentW = array.count *(imageW+2*space);
    //不允许在垂直方向上进行滚动
    self.scrollview.contentSize = CGSizeMake(contentW, imageH);

    //    3.设置分页
    self.scrollview.pagingEnabled = YES;
    
    //    4.监听scrollview的滚动
    self.scrollview.delegate = self;
    
    CGFloat width = 16*array.count;
    self.pageControl.frame = CGRectMake((KVIEWwith-width)/2, self.scrollview.frame.size.height-20, width, 10);
    _pageControl.numberOfPages = [array count];
    isshow = NO;
    self.array = array;
    [self.timer invalidate];
    [self addTimer];
}
@end

